/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package APP2.bookshop.controller;

import APP2.bookshop.author.Author;
import APP2.bookshop.book.Book;
import APP2.bookshop.book.Book.BOOKTYPE;
import APP2.bookshop.main.BookShop;
import APP2.bookshop.publisher.Publisher;
import java.util.Date;
import java.util.Set;

/**
 *
 * @author finalfire
 */
public class BookController {

    private BookShop bookshop;

    public BookController(BookShop bookshop) {
        this.bookshop = bookshop;
    }

    public boolean addBook(String title, int[] authors, BOOKTYPE type, String url, String publisher) {
        Book b = new Book(title, url, new Date(), type);

        // authors
        for (int i = 0; i < authors.length; i++) {
            b.setAuthor(this.bookshop.getAuthors().get(authors[i]));
            this.bookshop.getAuthors().get(authors[i]).setBook(b);
        }

        // publisher
        Publisher p = this.bookshop.getPublisherController().getPublisherByName(publisher);
        b.setPublisher(p);
        p.setBook(b);

        // add the book into the BookShop
        this.bookshop.getBooks().add(b);

        System.out.println("SIZE: " + this.bookshop.getBooks().size());

        return true;
    }

    public boolean deleteBook(Book b) {
        // delete this book from publisher
        Publisher p = b.getPublisher();
        if (p.getBooks().remove(b)) {
            // delete from authors
            Set<Author> auths = b.getAuthor();
            for (Author a : auths)
                a.getBooks().remove(b);

            // delete the book
            if (this.bookshop.getBooks().remove(b))
                return true;
        }

        return false;
    }

    public Book getBookByTitle(String title) {
        Book corr = null;
        for (Book b : this.bookshop.getBooks())
            if (b.getTitle().equals(title))
                corr = b;
        return corr;
    }

    // Return an AbstractListModel for JList component
    public javax.swing.AbstractListModel getBooksModel() {
        // create a new array to save authors' name
        final String[] elements = new String[this.bookshop.getBooks().size()];
        System.out.println("SIZE DENTRO CONTROLLER: " + elements.length);
        for (int i = 0; i < elements.length; i++)
            elements[i] = this.bookshop.getBooks().get(i).getTitle();

        // return a new AbstractListModel
        return UtilController.getListModelFromArray(elements);
    }

    public javax.swing.AbstractListModel getAllAuthorsByBook(String title) {
        return this.getAllAuthorsByBook(this.getBookByTitle(title));
    }

    public javax.swing.AbstractListModel getAllAuthorsByBook(Book b) {
        String[] authors = new String[b.getAuthor().size()];
        int i = 0;
        for (Author a : b.getAuthor()) {
            authors[i] = a.getName();
            i++;
        }
        return UtilController.getListModelFromArray(authors);
    }
}
